<template>
  <div>
    <!--    搜索角色信息-->
    <div id="search" style="text-align: right">
      <el-form :inline="true" id="demo-form-inline">
        <el-form-item label="角色名称" style="margin-right: 100px">
          <el-input @keyup.enter.native="handleSearch" v-model="formDate.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input @keyup.enter.native="handleSearch" v-model="formDate.descp" placeholder="请输入角色描述"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 100px;margin-right: 150px;">
          <el-button style="width: 150px;" type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <!--    展示角色信息-->
    <div id="list">
      <el-table :data="tableData" style="width: 100%" :stripe="true" :border="true"
                :highlight-current-row="true" height="450">
        <el-table-column label="角色编号" width="150px" prop="id"></el-table-column>
        <el-table-column label="角色名称" prop="tag">
          <template v-slot="scope">
            <el-tag
              :type="scope.row.name==='ROLE_super_admin'? 'danger' : (scope.row.name==='ROLE_boss')?'warning':'info'"
              disable-transitions>{{ scope.row.name }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="角色描述" prop="descp"></el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="scope">
            <el-button size="mini" @click="change(scope.row.id)">修改</el-button>
            <el-button size="mini" type="danger" @click="open(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    修改角色信息-->
    <el-dialog title="修改角色信息" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="角色编号" :label-width="formLabelWidth">
          <el-input placeholder="请输入内容" v-model="role.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="role.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="role.descp" placeholder="请输入权限描述"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="角色权限">
          <el-tree ref="tree"
            style="border: 1px solid #DCDFE6;border-radius: 4px"
                   :props="props"
                   :data="permissionList"
                   :default-checked-keys="defaultIdList"
                   node-key="id"
                   show-checkbox
                   :check-strictly="true"
                   @check-change="handleCheckChange">
          </el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--    分页-->
    <div id="block">
      <el-pagination background
                     :current-page.sync="pagination.page"
                     :page-size="pagination.pageSize"
                     :total="pagination.total"
                     :page-sizes="[7,15,30]"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     layout="total, prev, pager, next, sizes">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import permissionApi from "@/api/permission";
import roleApi from "@/api/role";
import qs from "qs";

export default {
  name: "list",
  data() {
    return {
      props: {
        label: 'tag',
      },
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      formDate: {
        name: '',
        descp: "",
      },
      pagination: {
        total: 0,
        page: 1,
        pageSize: 7
      },
      timeout: null,
      role: {
        id: "",
        name: "",
        descp: "",
        permissionIdList:[]
      },
      defaultIdList:[],
      permissionList:[],
    }
  },
  mounted() {
    this.queryRole()
  },
  methods: {
    //树形改变之后
    handleCheckChange() {
      this.role.permissionIdList=[]
      this.role.permissionIdList.push(this.$refs.tree.getCheckedKeys())
    },
    //查询树形权限
    selectTreePermission() {
      permissionApi.selectTreeList().then(resp => {
        this.permissionList = resp.data;
      }).catch(() => {
        this.$message.error('查询角色列表出错')
      })
    },
    //页码改变时的回调函数
    handleCurrentChange(page) {
      this.pagination.page = page;
      this.queryRole();
    },
    //页面大小改变时的回调函数
    handleSizeChange(pageSize) {
      this.pagination.pageSize = pageSize;
      this.queryRole();
    },
    //打开确认删除的提示框
    open(id) {
      this.$confirm('此操作删除该角色, 是否继续?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id);
      })
    },
    //点击修改按钮
    change(id) {
      this.selectRoleById(id)
      this.dialogFormVisible = true;
    },
    //修改权限信息函数
    handleEdit() {
      const updateMSG={
        id:this.role.id,
        name:this.role.name,
        descp:this.role.descp,
        permissionIdList:this.role.permissionIdList,
        defaultIdList:this.defaultIdList
      }
      roleApi.updateRole(qs.stringify(updateMSG,{indices: false})).then(resp => {
        if (resp.code === 6666) {
          this.$message.success("修改成功！")
          this.queryRole()
          this.role.permissionIdList=[]
          this.dialogFormVisible = false;
        } else {
          this.$message.info(resp.msg)
          this.role.permissionIdList=[]
        }
      }).catch(() => {
        this.$message.error("修改失败！")
        this.role.permissionIdList=[]
      });
    },
    //删除权限信息函数
    handleDelete(id) {
      roleApi.deleteRole(id).then(resp => {
        if (resp.code === 6666) {
          this.$message.success('删除成功!');
          this.queryRole();
        } else {
          this.$message.info('删除失败')
        }
      }).catch(error => {
        this.$message.error('删除出错');
      })
    },
    //点击查询时回到第一页
    handleSearch() {
      this.pagination.page = 1;
      this.queryRole()
    },
    //分页查询角色链表
    queryRole() {
      const pagination = {
        name: this.formDate.name,//搜索框中的值
        descp: this.formDate.descp,
        page: this.pagination.page,//当前页码
        pageSize: this.pagination.pageSize,//每页多少条数据
      }
      roleApi.selectRoleList(pagination).then(resp => {
        if (resp.code === 6666) {
          this.tableData = resp.data;
          this.pagination.page = resp.pageInfo.page;
          this.pagination.pageSize = resp.pageInfo.pageSize;
          this.pagination.total = resp.pageInfo.total;
        } else {
          this.$message.info('获取角色列表失败')
        }
      }).catch(() => {
        this.$message.error("查询角色列表出错！")
      })
    },
    //取消修改的提示信息
    handleCancle() {
      this.dialogFormVisible = false;
      this.role.permissionIdList=[]
    },
    //根据id查询权限信息
    selectRoleById(id) {
      this.defaultIdList=[];
      roleApi.selectRoleById(id).then(resp => {
        if (resp.code === 6666) {
          this.role.id = resp.data.id
          this.role.name = resp.data.name
          this.role.descp = resp.data.descp
          resp.data.permissionList.forEach(permission=>{
            this.defaultIdList.push(permission.id)
          })
          permissionApi.selectTreeList().then(resp => {
            this.permissionList = resp.data;
          })
        } else {
          this.$message.warning('查询角色信息失败')
        }
      }).catch(() => {
        this.$message.error('查询角色信息出错')
      })
    }
  },
}
</script>
<style scoped>

#list {
  margin-left: 20px;
  margin-right: 20px;
}

#search {
  margin-left: 100px;
  margin-top: 20px;
}

#block {
  margin-top: 20px;
  margin-left: 20px;
}

#demo-form-inline {
  margin-bottom: 0;
}

.el-divider {
  margin: 10px 0;
}
</style>
